package com.byl.sdk.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/9.
 */
public class JuheUtils {
    public static final String DEF_CHATSET = "UTF-8";
    public static final int DEF_CONN_TIMEOUT = 30000;
    public static final int DEF_READ_TIMEOUT = 30000;
    public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";

    //违章查询接口
    public static String WEIZHANG_URL = "http://v.juhe.cn/wz/query";
    // 汽车品牌列表
    public static String ESCJZPG_URL = "http://v.juhe.cn/usedcar/brand";
    //车系
    public static String CX_URL = "http://v.juhe.cn/usedcar/series";
    //车型
    public static String CHEXING_URL="http://v.juhe.cn/usedcar/car";
    //省份列表
    public static String SF_URL = "http://v.juhe.cn/usedcar/province";
    //城市列表
    public static String CS_URL="http://v.juhe.cn/usedcar/city";
    //精准评估
    public static String PG_URL="http://v.juhe.cn/usedcar/assess";
    //违章查询appkey
    public static final String WEIZHANG_APPKEY = "b16e413fdb33fb745b8b4504e17d605d";
    //二手车价值评估
    public static final String ESCJZPG_APPKEY = "3c1e72a718f34595ef1f27db8d080971";

    /**
     * 查询车辆违章信息
     *
     * @param map
     * @return
     */
    public static String queryWeiZhang(Map map) {
        Map params = new HashMap();//请求参数
        params.put("dtype", "json");//返回数据格式：json或xml或jsonp,默认json
        params.put("callback", "");//返回格式选择jsonp时，必须传递
        params.put("key", WEIZHANG_APPKEY);//你申请的key
        //params.put("province", "HN");//城市代码 *
        params.put("hphm", encodeStr(map.get("carNum").toString()));//号牌号码 完整7位 ,需要utf8 urlencode*
        params.put("hpzl", map.get("carNumCategory"));//号牌类型，默认02
        params.put("engineno", map.get("engineno"));//发动机号 (根据城市接口中的参数填写)
        params.put("classno", map.get("classno"));//车架号 (根据城市接口中的参数填写)
        return HttpUtils.post(WEIZHANG_URL, params);
    }

    /**
     * @param vehicle 车辆类型：passenger(乘用车)/commercial(商用车)
     * @return
     */
    public static String querybrand(String vehicle) {
        if (StringUtil.isEmpty(vehicle))
            vehicle = "passenger";
        String url = ESCJZPG_URL + "?vehicle=" + vehicle + "&key=" + ESCJZPG_APPKEY;
        return HttpUtils.get(url);
    }

    /**
     * 查询车系
     *
     * @param brand
     * @return
     */
    public static String querySeries(String brand) {
        String url = CX_URL + "?brand=" + brand + "&key=" + ESCJZPG_APPKEY;
        return HttpUtils.get(url);
    }

    /**
     * 查询省份列表
     * @return
     */
    public static String queryProvince() {
        String url = SF_URL + "?key=" + ESCJZPG_APPKEY;
        return HttpUtils.get(url);
    }

    /**
     * 查询城市列表
     * @param province
     * @return
     */
    public static String queryCity(String province){
        String url=CS_URL+"?province="+province+"&key="+ESCJZPG_APPKEY;
        return HttpUtils.get(url);
    }

    /**
     * 查询车型
     * @param series
     * @return
     */
    public static String queryCx(String series){
       String url=CHEXING_URL+"?series="+series+"&key="+ESCJZPG_APPKEY;
       return HttpUtils.get(url);
    }

    /**
     * 查询评估
     * @param map
     * @return
     */
    public static String queryAssess(Map map){
        String url=PG_URL+"?key="+ESCJZPG_APPKEY;
        return HttpUtils.post(url,map);
    }

    public static String encodeStr(String str) {
        String rs = null;
        try {
            rs = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
